<template>
  <div>
    <el-dialog
      :title="cardTitle"
      :visible.sync="cardStatus"
      width="80%"
      @close="close"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
    >
      <el-row class="button_group">
        <el-upload
          :action="uploadImageAction"
          accept="image/jpeg,image/png,image/jpg,image/gif"
          :on-success="uploadImageSuccess"
          :before-upload="uploadImageBefore"
          :show-file-list="false"
          multiple
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
        <el-button
          :type="choiceImages.length == 0 ? '' : 'success'"
          size="small"
          @click="choiceImageConfirm"
          class="choiceImageConfirm"
          :disabled="choiceImages.length == 0 ? true : false"
          >使用选中图片</el-button
        >
        <el-button size="small" type="danger" @click="close">关闭</el-button>
      </el-row>

      <div class="filter">
        <div class="select">
          <el-divider>分组</el-divider>
          <div
            v-for="item in imageFormatArr"
            :key="item.id"
            class="item"
            @click="changeImageFormat(item.id)"
            :class="imageFormat == item.id ? 'active' : ''"
          >
            {{ item.title }}
          </div>
        </div>
        <div class="box" v-loading="imageLoading">
          <div v-for="item in imageData.data" :key="item.id" class="image_box">
            <el-image
              :src="item.url"
              @click="choiceImage(item)"
              class="image"
              :class="item.choice == item.id ? 'active' : ''"
              fit="contain"
            ></el-image>
            <div v-if="item.choice == 1" class="badge">
              {{ item.imageSort }}
            </div>
          </div>
        </div>
      </div>

      <el-pagination
        background
        style="text-align: center; margin: 20px 0"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="imageData.total"
        @size-change="changeImageSize"
        @current-change="changeImagePage"
      ></el-pagination>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "SumpleImageUpload",
  data() {
    return {
      imageFormat: "", // 图片类型
      choiceImages: [], // 选中的图片
      pageSizes: [50, 100, 200, 500, 800, 1000, 2000],
      searchParams: {
        page: 1,
        limit: 50,
        format: ""
      }
    };
  },
  methods: {
    changeImageSize(limit) {
      this.$emit("changeImageSize", limit);
    },
    changeImagePage(page) {
      this.$emit("changeImagePage", page);
    },
    changeImageFormat(format) {
      this.imageFormat = format;
      this.searchParams.page = 1;
      this.$emit("changeImageFormat", format);
    },
    // 确定选中图片
    choiceImageConfirm() {
      this.$emit("choiceImageConfirm", this.choiceImages);
      this.init();
    },
    //选中图片
    choiceImage(item) {
      if (this.choiceImages.length == 1 && !this.multiple && !item.choice) {
        this.$message.error("只能选择一个图片");
        return false;
      }
      if (item.choice) {
        item.choice = 0;
      } else {
        item.choice = 1;
      }
      var imageSort = 0; // 选中的图片序号
      this.choiceImages = []; // 选中的图片
      for (let i = 0; i < this.imageData.data.length; i++) {
        if (this.imageData.data[i].choice == 1) {
          this.imageData.data[i].imageSort = ++imageSort;
          this.choiceImages.push(this.imageData.data[i].url);
        }
      }
    },
    close() {
      this.init();
    },
    init() {
      this.imageFormat = "";
      this.searchParams = {
        page: 1,
        limit: 50,
        format: ""
      };
      this.choiceImages = [];
      this.$emit("closeImage"); // cardStatus必须由父组件关闭
    },
    uploadImageSuccess(res) {
      this.$emit("uploadImageSuccess", res);
    },
    uploadImageBefore() {
      this.$emit("uploadImageBefore");
    }
  },
  props: {
    // 卡片显示/隐藏状态
    cardStatus: {
      type: Boolean,
      default: false
    },
    // 卡片标题
    cardTitle: {
      type: String,
      default: "请选择图片"
    },
    // 图片分类数组
    imageFormatArr: {
      type: Array,
      default: []
    },
    // 图片数据
    imageData: {
      type: Object,
      default: {}
    },
    // 图片加载loading
    imageLoading: {
      type: Boolean,
      default: false
    },
    // 是否支持多选
    multiple: {
      type: Boolean,
      default: true
    },
    // 图片上传地址
    uploadImageAction: {
      type: String,
      default: ""
    }
  }
};
</script>

<style scoped lang="less">
.button_group {
  display: flex;
  margin-bottom: 10px;
  .choiceImageConfirm {
    margin-left: 10px;
  }
}
.filter {
  display: flex;
  justify-content: space-between;
  .select {
    display: flex;
    flex-direction: column;
    border-right: 1px solid #eee;
    width: 150px;
    margin-right: 10px;
    padding-right: 10px;
    .item {
      width: 100%;
      margin-bottom: 5px;
      height: 30px;
      line-height: 30px;
      text-align: center;
      font-size: 14px;
      cursor: pointer;
      border-radius: 5px;
      color: #409eff;
      &.active {
        background: #409eff;
        color: #fff;
      }
    }
  }
  .box {
    display: flex;
    flex-wrap: wrap;
    overflow: scroll;
    flex: 1;
    max-height: 600px;
    justify-content: flex-start;
    align-items: flex-start;
    .image_box {
      position: relative;
      // box-sizing: border-box;
      width: 100px;
      margin: 0 5px 10px 5px;
      max-height: 100px;
      .badge {
        width: 20px;
        height: 20px;
        background-color: #67c23a;
        color: #fff;
        border-radius: 50%;
        text-align: center;
        line-height: 20px;
        position: absolute;
        top: 15px;
        right: 15px;
      }
      .image {
        width: 100px;

        // border-radius: 5px;
        // box-sizing: border-box;
        &.active {
          border: 2px solid #67c23a;
        }
      }
    }
  }
}
</style>
